﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class ManageFunds : Form
    {
        BindingSource _bindsrc = new BindingSource();

        #region Constructor/Load
        public ManageFunds()
        {
            InitializeComponent();
        }

        private void ManageFunds_Load(object sender, EventArgs e)
        {
            _bindsrc.DataSource = MainForm.projector.Funds;
            lstFunds.DataSource = _bindsrc;
        }
        #endregion

        #region Add/Remove Funds
        private void btnNewFund_Click(object sender, EventArgs e)
        {
            Fund _fund = new Fund(new Balance(), "NewFund", FundClassEnum.Savings, false);
            MainForm.projector.Funds.Add(_fund);
            propertyGrid1.SelectedObject = _fund;
            _bindsrc.ResetBindings(false);

            dtpInceptDate.Value = DateTime.Now.Date;
            txtInceptAmount.Text = "0.00";
        }

        private void btnDeleteFund_Click(object sender, EventArgs e)
        {
            if (lstFunds.SelectedItem == null)
            {
                MessageBox.Show("No fund selected to delete");
                return;
            }

            MainForm.projector.Funds.Remove((Fund) lstFunds.SelectedItem);
        }

        private void lstFunds_SelectedIndexChanged(object sender, EventArgs e)
        {
            propertyGrid1.SelectedObject = lstFunds.SelectedItem;

            if (propertyGrid1.SelectedObject != null)
            {
                Fund f = (Fund)propertyGrid1.SelectedObject;
                if (f.InceptionBalance.BalanceDate != DateTime.MinValue)
                    dtpInceptDate.Value = f.InceptionBalance.BalanceDate;
                txtInceptAmount.Text = f.InceptionBalance.Amount.ToString();
            }
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSetInception_Click(object sender, EventArgs e)
        {
            Fund f;

            Decimal amt;

            try { amt = Decimal.Parse(txtInceptAmount.Text); }
            catch
            {
                lblInceptAmount.ForeColor = Color.Red;
                return;
            }
            lblInceptAmount.ForeColor = Color.FromKnownColor(KnownColor.ControlText);

            if (propertyGrid1.SelectedObject != null)
            {
                f = (Fund)propertyGrid1.SelectedObject;

                f.InceptionBalance.BalanceDate = dtpInceptDate.Value;
                f.InceptionBalance.Amount = amt;
            }
        }
    }
}
